package pers.hl.module_main.common

import android.util.Log
import android.view.LayoutInflater
import androidx.annotation.LayoutRes
import androidx.viewbinding.ViewBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import pers.hl.lib_global.KTApplication
import pers.hl.module_main.R
import java.lang.reflect.ParameterizedType

@Suppress("UNCHECKED_CAST")
abstract class BaseVBAdapter<T, VH : BaseViewHolder, VB : ViewBinding>(
    @LayoutRes private val layoutResId: Int,
    data: MutableList<T>? = null
) : BaseLibQuickAdapter<T, VH>(layoutResId, data) {

    protected lateinit var mViewBinding: ViewBinding

    init {
        initViewBinding()
    }

    private fun initViewBinding() {
        Log.i(TAG, "initViewBinding")
        val type = javaClass.genericSuperclass
        // ParameterizedType 参数化类型 声明类型中带有“<>”的都是参数化类型
        if (type is ParameterizedType) {
            val clazz = type.actualTypeArguments[2] as Class<VB>
            val method = clazz.getMethod("inflate", LayoutInflater::class.java)
            val layoutInflater = LayoutInflater.from(KTApplication.getInstance())
            mViewBinding = method.invoke(null, layoutInflater) as VB
            Log.i(TAG, "initViewBinding finish.")
        }
    }


}